package com.dh.project.utils;

import com.aliyuncs.exceptions.ClientException;
import com.dh.project.model.Coupon;
import com.dh.project.model.CustomerCoupon;
import com.dh.project.model.CustomerInfo;
import com.dh.project.model.Insurance;
import com.dh.project.service.CouponService;
import com.dh.project.service.CustomerCouponService;
import com.dh.project.service.CustomerInfoService;
import com.dh.project.service.CustomerInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/15.
 */

@Component  //import org.springframework.stereotype.Component;
 class timeTask {

    private CustomerCouponService customerCouponService;

    private CouponService couponService;

    private CustomerInsuranceService customerInsuranceService;

    private CustomerInfoService customerInfoService;

    @Autowired
    timeTask (CustomerCouponService customerCouponService,CouponService couponService,CustomerInfoService customerInfoService){
        this.customerCouponService = customerCouponService;
        this.couponService = couponService;
        this.customerInsuranceService = customerInsuranceService;
        this.customerInfoService = customerInfoService;
    }

//    @Scheduled(cron="0 15 23 * * ?")   //每天23点15分
    @Scheduled(cron="1 0 0 * * ?")   //每天00:00:01
//    @Scheduled(cron="0/30 * * * * ?")   //每隔5秒
    public void timeTask() {
        //更新优惠券是否过期

        //当天时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
//        System.out.println("测试："+date.substring(0,7));
        Map<String, String> map = new HashMap<String, String>();
        map.put("date", date);

        Map map1 = new HashMap();
        List<Coupon> couponList = couponService.selectByMap(map1);

        for (int i = 0; i < couponList.size(); i++) {
            if (new Date().getTime() > Long.valueOf(couponList.get(i).getUseful_time())){
                Map map2 = new HashMap();
                map2.put("coupon_id",couponList.get(i).getId());
                List<CustomerCoupon> customerCoupons = customerCouponService.selectByMap(map2);
                for (int j = 0; j < customerCoupons.size(); j++) {
                    customerCoupons.get(j).setIs_use(2);
                    customerCouponService.updateById(customerCoupons.get(j));
                }
            }
        }
    }
    @Scheduled(cron = "0 0 0 * * ? ")
    public void doJob(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        Map map = new HashMap<String, String>();
        map.put("lastDate",date);
        List<Insurance> list = customerInsuranceService.selectByMap(map);
        for (Insurance insurance: list) {
            insurance.setLastDate("0");
            insurance.setStatus(0);
            customerInsuranceService.updateById(insurance);

        }


    }
    @Scheduled(cron = "0 0 0 * * ? ")
    public void sms() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Map map1 = new HashMap();
        List<Integer> integers = customerCouponService.selectUseful();
        for (Integer a:integers) {
            Coupon coupon = couponService.selectById(a);

            if(new Date().getTime()-Long.parseLong(coupon.getUseful_time())/86400000==30){
                String time = sdf.format(new Date(Long.parseLong(coupon.getUseful_time())));
                map1.put("couponTime",time);
                map1.put("money",coupon.getPrice());
                List<CustomerCoupon> customerCoupons = customerCouponService.selectCustomerCouponById(a);
                for (CustomerCoupon cs:customerCoupons) {
                    CustomerInfo customerInfo = customerInfoService.selectById(cs.getCustomer_id());
                    try {
                        //尊敬的用户，您有1张价值${money}元的现金券将于${couponTime}失效，抓住机会，不要浪费

                        DayuSMSUtil.sendSms(map1.toString(),DayuSMSUtil.APPCOUPON,customerInfo.getCustomer_phone());
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        }




    }



